----------------------------
-- Simplistic Street
-- by Jammie Dodger
--
-- Project Started - October 20, 2009
-- Completion -None
----------------------------

require("glon")

hook.Add("PlayerSpawn", "LoadInventory", function(ply)
	ply:LoadInventory()
	ply:SendInventory()
end)

function _R.Player:SaveInventory()
	if self.Inventory then
		local WriteItems = glon.encode(self.Inventory)
		file.Write("SS/Inventory/User_"..self:UniqueID().."/Items.txt", WriteItems)
	end
end	

function _R.Player:LoadInventory()
	if file.Exists("SS/Inventory/User_"..self:UniqueID().."/Items.txt") then
		local LoadItems = file.Read("SS/Inventory/User_"..self:UniqueID().."/Items.txt")
		self.Inventory = glon.decode(LoadItems)
	else		
		self.Inventory = {}
	end
	
end

function _R.Player:SendInventory()
	umsg.Start("SetItems", self)
		umsg.Char(table.Count(self.Inventory))
		
		for k,v in pairs(self.Inventory) do
			umsg.String(k)
			umsg.Short(v)
		end
	umsg.End()
end

function _R.Player:HasItem(Item)
	return (self.Inventory[Item] and self.Inventory[Item] > 0) or false
end

function _R.Player:AddItem(Item, Amount)

	local LowerItem = string.lower(Item)
	local Inventory = self.Inventory
	
	if Inventory[LowerItem] and Amount > 0 then
		Inventory[LowerItem] = Inventory[LowerItem] + Amount
		self:SendInventory()
		
	elseif Amount > 0 and not Inventory[LowerItem] then
		Inventory[LowerItem] = Amount
		self:SendInventory()			
	
	elseif !Amount then
		print("Error!")
	end
end

function _R.Player:RemoveItem(Item, Amount)

	local LowerItem = string.lower(Item)
	local Inventory = self.Inventory
	
	if Inventory[LowerItem] and Amount > 0 then
		Inventory[LowerItem] = Inventory[LowerItem] - Amount
		self:SendInventory()
		
	elseif Amount > 0 and not Inventory[LowerItem] or Inventory[LowerItem] == 0 then
		Inventory[LowerItem] = 0
		self:SendInventory()			
	
	elseif !Amount then
		print("Error!")
	end
end


function _R.Player:SetItems(Item, Amount)

	local LowerItem = string.lower(Item)
	local Inventory = self.Inventory

	if Inventory and LowerItem and Amount then
		if type(LowerItem) == "string" and type(Amount) == "number" then
			self.Inventory[LowerItem] = Amount
			self:SendInventory()
		else
			print("Player:SetItems used incorrectly!")
		end
	end
end
	
	
local function DropItems(ply, cmd, args)
	local Item = args[1]
	local Info = Items[Item]
	
	if !Item or ply.Inventory[Item] == nil then
			print("Dropping Items does not work!")
		return
	end
	
	if tonumber(ply.Inventory[Item]) < 1 then
			print("There are too low items!")
		return
	end
	
	local pos = ply:EyePos()
	local tracedata = {}
	tracedata.start = pos
	tracedata.endpos = pos+(ply:GetForward()*150)
	tracedata.filter = ply
	local tr = util.TraceLine(tracedata)
	
	if Item == "prop_physics" then
		local ent = ents.Create(Item)
		ent:SetPos(tr.HitPos + tr.HitNormal*10)
		ent:SetModel(Info.EntityModel or "models/error.mdl")
		ent:Spawn()
		ent:Activate()
		ply:RemoveItem(Item, 1)
	else
		local ent = ents.Create(Item)
		ent:SetPos(tr.HitPos + tr.HitNormal*10)
		ent:Spawn()
		ent:Activate()
		ply:RemoveItem(Item, 1)
	end
end
concommand.Add("ss_dropitem", DropItems)

local function UseItem(ply, cmd, args)
	local Item = args[1]
	if !Item or ply.Inventory[Item] == nil then
			print("Invalid shit")
		return
	end
	
	if ply.Inventory[Item] < 1 then
		print("Shit, bugger.")
		return
	end

	if !ply.Inventory[Item].Useable then
		print("Bleurgh")
		return
	end
	
	local ent = ents.Create(Item)
	ent:SetPos(ply:GetPos())
	ent:Spawn()
	ent:Activate()
	ent:Use(ply, ply)

	ply:RemoveItem(Item, 1)
end
concommand.Add("ss_useitem", UseItem)


local function UseInventoryObjects(ply, ent)

	if ply:KeyDown(IN_WALK) and Items[ent:GetClass()] then
		if !ply.Inventory[ent:GetClass()] then
			ply.Inventory[ent:GetClass()] = 0
		end
		
		local max = Items[ent:GetClass()]["Limit"]
		
		if max > 0 and ply.Inventory[ent:GetClass()] >= max then
			print("Max Reached")
			return false
		end
		
		ply:AddItem(ent:GetClass(), 1)
		ply:EmitSound("items/ammo_pickup.wav", 100, 100)
		
		local vec = ply:GetPos() - ent:GetPos()
		ent:GetPhysicsObject():ApplyForceCenter(Vector(0,0,450))
		ent:SetNotSolid(true)
		
		local ed = EffectData()
		ed:SetEntity(ent)
		util.Effect( "propspawn", ed )	
		
		SafeRemoveEntityDelayed(ent, 0.75)

		return false
	end

	if ply:KeyDown(IN_WALK) then
		return false
	end
	return true
end
hook.Add("PlayerUse", "UseInventory", UseInventoryObjects)

